import BaseLoad from "@/assets/vue-mixin/BaseLoad";
import editReceive from "../edit/edit-receive";
import editCheck from "../edit/edit-check";
import contractView from "@/views/rent/contract/view.vue";
import editActual from "../edit/edit-actual";
import editData from "../edit/edit-data";
import editReceivable from "../edit/edit-receivable";
import leadData from "../edit/lead-data";
import showActual from "../edit/show-actual";
import editAccounts from "../edit/edit-accounts";
import {
    fetchList,
    confirmPnNum,
    getRentContractPageList,
    handlePress,
    sendPnNum
} from "../../../api/finance-collection-receivable";

export default {
    name: "index",
    components: {
        editReceive,
        editCheck,
        contractView,
        editActual,
        editData,
        editReceivable,
        leadData,
        showActual,
        editAccounts
    },
    mixins: [BaseLoad],
    provide() {
        return {
            // 更新核算列表
            initList: this.getListData,
            // 更新合同列表
            updateList: this.getContractList
        };
    },
    beforeMount() {
        this.getContractList();
    },
    data() {
        return {
            contract: {
                // 列表数据
                tableData: [],
                // 请求参数
                params: {
                    current: 1,
                    size: 10,
                    projectName: ""
                },
                // 分页
                pager: {
                    total: 0, // 总页数
                    current: 1, // 当前页数
                    size: 10 // 每页显示多少条
                },
                // 页面loading
                loading: false
            },
            tableData: [],
            // 请求参数
            params: {
                current: 1,
                size: 10,
                projectName: ""
            },
            // 选中的数据
            selectionList: [],
            // 分页
            pager: {
                total: 0, // 总页数
                current: 1, // 当前页数
                size: 10 // 每页显示多少条
            },
            // 页面loading
            loading: false,
            filterList: [
                {
                    label: "收款项目",
                    inputType: "select",
                    name: "chargeProjectName",
                    multiple: true,
                    children: [
                        {
                            label: "租金",
                            value: "租金"
                        },
                        {
                            label: "租赁押金",
                            value: "租赁押金"
                        },
                        {
                            label: "装修保证金",
                            value: "装修保证金"
                        }
                    ]
                },
                {
                    label: "费用周期",
                    inputType: "date",
                    name: "costPeriodStart"
                },
                {
                    label: "应收日期",
                    inputType: "daterange",
                    name: "receivableDate"
                },
                {
                    label: "结算状态",
                    inputType: "select",
                    name: "payStatus",
                    multiple: true,
                    children: [
                        {
                            label: "未支付",
                            value: "01"
                        },
                        {
                            label: "已支付",
                            value: "02"
                        }
                    ]
                },
                {
                    label: "发送状态",
                    inputType: "select",
                    name: "sendStatus",
                    multiple: true,
                    children: [
                        {
                            label: "未确认",
                            value: "01"
                        },
                        {
                            label: "待发送",
                            value: "03"
                        },
                        {
                            label: "已发送",
                            value: "02"
                        }
                    ]
                }
            ],
            flag: false,
            btnText: "未接收",
            operText: "接收",
            clickCheck: false,
            activeIndex: -1,
            NewIcon: require("@/assets/images/new.png")
        };
    },
    methods: {
        // 获取合同列表
        getContractList(params = {}) {
            this.contract.loading = true;
            let requestData = { ...this.contract.params, ...params };
            getRentContractPageList(requestData)
                .then(resp => {
                    const { current, size, total, records } = resp.data.data;
                    this.contract.tableData = records.map((item, index) => ({
                        ...item,
                        active: index === 0
                    }));
                    this.contract.params = requestData;
                    this.contract.pager = { total, current, size };
                    this.changeAccountList(
                        this.contract.tableData[0].rentContractId
                    );
                })
                .catch(() => {
                    this.tableLoading = false;
                })
                .finally(() => {
                    this.contract.loading = false;
                });
        },
        //切换
        changeAccountList(rentContractId) {
            this.contract.tableData = this.contract.tableData.map(item => ({
                ...item,
                active: item.rentContractId == rentContractId
            }));
            if (
                rentContractId === undefined ||
                rentContractId === null ||
                rentContractId === ""
            ) {
                if (this.contract.tableData.length > 0) {
                    this.params.rentContractId = rentContractId;
                    this.getListData();
                }
            } else {
                this.params.rentContractId = rentContractId;
                this.getListData();
            }
        },
        // 获取右边列表
        getListData(params = {}) {
            this.loading = true;
            let requestData = { ...this.params, ...params };
            fetchList(requestData)
                .then(res => {
                    const { current, size, total, records } = res.data.data;
                    this.tableData = records;
                    this.params = requestData;
                    this.pager = { total, current, size };
                })
                .finally(() => {
                    this.loading = false;
                });
        },
        restartSelectionList() {
            //不要带过去，让其自动生成
            this.selectionList[0].createBy = null;
            this.selectionList[0].createName = null;
            this.selectionList[0].createTime = null;
            this.selectionList[0].operateBy = null;
            this.selectionList[0].operateName = null;
            this.selectionList[0].operateTime = null;
        },
        // 接收
        editReceive(item) {
            //this.activeIndex = index
            this.$refs.editReceive.show(item);
        },
        // 核算
        editCheck(item) {
            this.$refs.editCheck.show(item);
        },
        // 查看合同
        showData(contractId) {
            this.$refs.contractView.show(contractId);
            //this.$parent.$router.push({path: "/wy-rent/contract/view?id=" + contractId});
        },
        //收款
        editActual() {
            if (this.selectionList.length == 1) {
                if (this.selectionList.find(item => item.payStatus == "02")) {
                    this.err("该收款中存在已支付数据");
                    return;
                }
                if (
                    this.selectionList.find(item => item.chargeProjectId == "1")
                ) {
                    this.err("租金不得手动收款");
                    return;
                }
                let ids = this.selectionList.map(item => item.receivableId);
                //不要带过去，让其自动生成
                this.$refs.editActual.show(
                    this.selectionList[0].rentContractId,
                    ids
                );
            } else {
                this.err("请选择一个要收款的项目");
            }
        },
        //新建应收
        editData(item) {
            this.$refs.editData.show(item);
        },
        //合并收款
        handleMerge() {
            if (this.selectionList.length > 0) {
                if (this.selectionList.find(item => item.payStatus == "02")) {
                    this.err("该收款中存在已支付数据");
                    return;
                }
                let ids = this.selectionList.map(item => item.receivableId);
                //不要带过去，让其自动生成
                this.$refs.editActual.show(
                    this.selectionList[0].rentContractId,
                    ids
                );
            } else {
                this.err("请选择一个要收款的项目");
            }
        },
        //推送非税通知书
        sendPnNum() {
            if (this.selectionList.length === 0) {
                this.err("请选择一个要推送的项目");
            } else {
                if (
                    this.selectionList.find(item => item.chargeProjectId != "1")
                ) {
                    this.err("只能推送租金的数据");
                    return;
                }
                if (this.selectionList.find(item => item.sendStatus != "03")) {
                    this.err("只能推送待发送的数据");
                } else {
                    this.confirm("发送推送", "您确定要推送该数据吗？", () => {
                        let ids = this.selectionList.map(
                            item => item.receivableId
                        );
                        //this.tableLoading = true;
                        this.loadAction("请稍等...");
                        sendPnNum(ids.join(","))
                            .then(resp => {
                                if (resp.data.code === 0) {
                                    this.suc("操作成功");
                                    this.getListData();
                                    this.loadClose();
                                } else {
                                    this.err("操作失败");
                                    this.loadClose();
                                }
                                //this.loadClose()
                                //this.tableLoading = false;
                            })
                            .catch(e => {
                                this.loadClose();
                                this.err(e);
                            });
                    });
                }
            }
        },
        //确认推送非税单
        confirmPnNum() {
            if (this.selectionList.length === 0) {
                this.err("请选择一个要确认的项目");
            } else {
                if (
                    this.selectionList.find(item => item.chargeProjectId != "1")
                ) {
                    this.err("只能确认租金的数据");
                    return;
                }
                if (this.selectionList.find(item => item.sendStatus != "01")) {
                    this.err("只能确认未确认的数据");
                } else {
                    this.confirm("确认", "您确定该数据吗？", () => {
                        let ids = this.selectionList.map(
                            item => item.receivableId
                        );
                        confirmPnNum(ids.join(",")).then(resp => {
                            if (resp.data.code === 0) {
                                this.suc("操作成功");
                                this.getListData();
                            } else {
                                this.err("操作失败");
                            }
                        });
                    });
                }
            }
        },
        //应收调整
        editReceivable() {
            if (this.selectionList.length === 1) {
                if (this.selectionList[0].sendStatus != "01") {
                    this.err("只能调整待确认的数据");
                    return;
                }
                if (this.selectionList[0].payStatus == "01") {
                    this.restartSelectionList();
                    this.$refs.editReceivable.show(this.selectionList[0]);
                } else {
                    this.err("只能调整未支付的应收信息");
                }
            } else {
                this.err("请选择一个要调整应收的项目");
            }
        },
        //立即催款
        handlePress() {
            if (this.selectionList.length === 1) {
                if (this.selectionList[0].payStatus == "01") {
                    if (this.selectionList[0].sendStatus != "02") {
                        this.err("只能催款已推送的数据");
                        return;
                    }
                    let receivableId = this.selectionList[0].receivableId;
                    handlePress(receivableId).then(resp => {
                        if (resp.data.data) {
                            // this.$message({message: '本次催缴成功', type: 'success'});
                            //this.suc('删除成功')
                            //this.getListData()
                            this.$notify({
                                title: "本次催缴成功！",
                                // message: '本次催缴成功！',
                                type: "success",
                                offset: 170
                            });
                        } else {
                            this.err("催缴失败");
                        }
                    });
                } else {
                    this.err("只能催款未支付的应收信息");
                }
            } else {
                this.err("请选择一个要立即催款的项目");
            }
        },
        //导入
        handleLeadingIn() {
            this.$refs.leadData.show();
        },
        //导出
        downloadData() {
            this.publicExport(
                "应收",
                "/api/financecollectionreceivable/downloadExcel",
                this.params
            );
        },
        //查看
        showReceivableData(item) {
            this.$refs.showActual.show(item);
        },
        //生成核算单
        editAccounts(item) {
            this.$refs.editAccounts.show(item);
        },
        handleInputSearch() {
            if (this.leftSearch) {
                this.leftSearchForm.projectName = this.leftSearch;
                // console.log('this.leftSearchForm---',this.$refs.notCheck, this.activeIndex)
                if (this.activeIndex !== -1) {
                    this.$refs.notCheck[this.activeIndex].innerHTML = "未接收";
                    this.$refs.notCheck[
                        this.activeIndex
                    ].style.backgroundColor = "#fff";
                }
                this.clickCheck = false;
            } else {
                delete this.leftSearchForm.projectName;
            }
            this.getContractList();
        },
        handleClickCheck() {
            this.$refs.notCheck[this.activeIndex].innerHTML = "已接收";
            this.$refs.notCheck[this.activeIndex].style.backgroundColor =
                "#1563d1";
            this.clickCheck = true;
        },
        // 菜单点击监听
        handleMenuClick(type) {
            switch (type) {
                case "test":
                    this.editActual();
                    break;
                case "test1":
                    this.editData();
                    break;
                case "test2":
                    this.confirmPnNum();
                    break;
                case "test3":
                    this.sendPnNum();
                    break;
                case "test4":
                    this.editReceivable();
                    break;
                case "test5":
                    this.handlePress();
                    break;
                case "export":
                    this.downloadData();
                    break;
                default:
                    this.$message(type);
                    break;
            }
        }
    },
    filters: {
        mySubString(val) {
            if (val != null) {
                return val.substring(0, 7);
            }
            return "";
        },
        ellipsis(value) {
            if (!value) {
                return "";
            } else if (value.length > 8) {
                return value.slice(0, 8) + "...";
            } else {
                return value;
            }
        }
    }
};
